<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片布局 -->
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-button type="primary">添加角色</el-button>
      </el-row>
      <!-- 角色列表区域 -->
      <el-table :data="rolelist" border>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              class="bdbottom vcenter"
              :class="{ bdtop: i1 === 0 }"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag
                  closable
                  @close="removeRightById(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级和三级权限 -->
              <el-col :span="19">
                <el-row
                  class="vcenter"
                  :class="{ bdtop: i2 !== 0 }"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <!-- 放二级 -->
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 放三级 -->
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限弹出的对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightsDialogVisible"
      @close="setRightsDialogClose"
      width="50%"
    >
      <!-- 树形组件
        show-checkbox:显示复选框
        node-key:设置选中节点对应的值
        default-expand-all:是否默认展开所有节点
        :default-checked-keys 设置默认选中项的数组
        ref:设置引用 -->
      <!-- 树形控件 -->
      <el-tree
        :data="rightlist"
        :props="treeProps"
        show-checkbox
        default-expand-all
        node-key="id"
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 所有角色列表数据
      rolelist: [],
      // 控制分配权限对话框的显示与隐藏
      setRightsDialogVisible: false,
      // 所有的权限列表
      rightlist: [],
      // 树形控件的属性绑定对象
      treeProps: {
        label: 'authName' // 树显示属性
      },
      // 设置树形控件中默认选中的内容
      defKeys: [],
      // 当前编辑的角色的id
      roleId: undefined
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    // 获取所有角色的数据
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      this.rolelist = res.data
    },
    // 根据id删除对应的权限
    removeRightById (role, rightId) {
      // 提示的删除弹框
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        //  对现有的角色权限进行更新 不再加载所有权限
        role.children = res.data
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {

      })
    },
    // 分配权限对话框
    async showSetRightDialog (role) {
      const { data: res } = await this.$http.get('rights/tree')
      this.roleId = role.id
      this.rightlist = res.data
      this.setRightsDialogVisible = true

      //  获取最后一级的id,方式1-不用递归
      //   role.children.forEach(item1 => {
      //     item1.children.forEach(item2 => {
      //       item2.children.forEach(item3 => {
      //         this.defKeys.push(item3.id)
      //       })
      //     })
      //   })
      this.getLeafKey(role, this.defKeys)
    },
    // 方法2-写简化版递归,递归过程中少传变量
    // getLeafKey (item) {
    //   if (item.children) {
    //     item.children.forEach(t => {
    //       this.getLeafKey(t)
    //     })
    //   } else {
    //     this.defKeys.push(item.id)
    //   }
    // }
    // 方法3 标准版 递归
    getLeafKey (item, arry) {
      if (item.children) {
        item.children.forEach(t => {
          this.getLeafKey(t, arry)
        })
      } else {
        arry.push(item.id)
      }
    },
    // 监听分配权限对话框的关闭事件
    setRightsDialogClose () {
      this.defKeys = []
    },
    // 点击确定按钮添加对应的权限
    async saveRights () {
      const rids = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      await this.$http.post('roles/' + this.roleId + '/rights', { rids: rids.join(',') })
      this.setRightsDialogVisible = false
      this.getRolesList()
      this.$message({
        message: '分配成功',
        type: 'success'
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px dashed #eee;
}
.bdbottom {
  border-bottom: 1px dashed #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
